package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.jpa.TblCampania;
import com.mastercard.assets.jpa.TblCategoria;
import com.mastercard.assets.jpa.TblCuentaPaquete;
import com.mastercard.assets.jpa.TblPaquete;
import com.mastercard.assets.jpa.TblSubcateg;
import com.mastercard.assets.jpa.TblTipoCampania;
import com.mastercard.assets.jpa.TblTipoCuenta;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AlertasForm;
import com.mastercard.assets.web.struts.forms.AssetAltaForm;
import com.mastercard.assets.web.struts.forms.AssetAsignarPaquetesForm;
import com.mastercard.assets.web.struts.forms.AssetBorrarForm;
import com.mastercard.assets.web.struts.forms.AssetBuscarForm;
import com.mastercard.assets.web.struts.forms.AssetEditarForm;
import com.mastercard.assets.web.struts.forms.AssetModificarPaquetesForm;
import com.mastercard.assets.web.struts.forms.CampanasPorAutorizarForm;
import com.mastercard.assets.web.struts.forms.FlujosFForm;
import com.mastercard.assets.web.struts.forms.FormasForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaRegistrarForm;
import com.mastercard.assets.web.struts.forms.LoginForm;
import com.mastercard.assets.web.struts.forms.TareasForm;
import com.mastercard.assets.web.struts.forms.TareasOperationForm;
import com.mastercard.assets.web.struts.forms.TareasRegistrarFormasSalidasForm;
import com.mastercard.assets.web.util.to.ManejadorPaquetesAsignados;
import com.mastercard.assets.web.util.to.PaqueteAutorizado;
import com.mastercard.assets.web.util.to.UserLogin;

public class TareasRegistrarFormasSalidas
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		TareasRegistrarFormasSalidasForm f = ( TareasRegistrarFormasSalidasForm ) form ;
		
		int cveTarea = f.getCveTarea( ) ;
		int cveFlujo = f.getCveFlujo( ) ;
		int cveSalida = f.getCveSalida( ) ;
		int[ ] formas = f.getFormas( ) ;
		
//		System.out.println( "TareasRegistrarFormasSalidas: " + cveSalida ) ;
//		System.out.println( "TareasRegistrarFormasSalidas: " + cveFlujo ) ;
//		System.out.println( "TareasRegistrarFormasSalidas: " + cveTarea ) ;
//		System.out.println( "TareasRegistrarFormasSalidas: " + formas ) ;
//		System.out.println( "TareasRegistrarFormasSalidas: " + ( ( formas == null ) ? 0 : formas.length ) ) ;
		
		try
		{
			MasterCardDAO.updateExitsOnWork( cveTarea , cveSalida , formas ) ;
		}catch( Exception e )
		{
                    MasterCardDAO.closeEntityManager( ) ;
			user.setMessageError( e.getMessage( ) ) ;
		}
		
		// obtiene la cantidad de alertas existentes
		int alertas = MasterCardDAO.getNumberOfAlertsOnExitWork( cveTarea , cveSalida ) ;
		
		// en caso de que haya alertas
		if( alertas > 0 )
			user.setNextWorkArea( "/TareasOperationIn.do?operation=" + 5 + "&cveFlujo=" + cveFlujo + "&cveTarea=" + cveTarea + "&cveSalida=" + cveSalida + "&cveRole=" + user.getActualRoleId( ) ) ;
		else
			user.setNextWorkArea( "/TareasOperationIn.do?operation=" + 6 + "&cveFlujo=" + cveFlujo + "&cveTarea=" + cveTarea + "&cveSalida=" + cveSalida + "&cveRole=" + user.getActualRoleId( ) ) ;
//		user.setNextWorkArea( "/TareasOperationIn.do?cveTarea=" + cveTarea + "&operation=4&cveFlujo=" + cveFlujo ) ;
		
                MasterCardDAO.closeEntityManager( ) ;
		return mapping.findForward( "success" ) ;
	}
	
}
